package com.lzx.adx.report.support.strategy.v2.channel;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@ProductType(ProductChannelConstant.FAN_LI)
public class FanLiReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        String dspExtend = accountDTO.getDspExtend();
        JSONObject jsonObject = JSONObject.parseObject(dspExtend);
        Map<String,Object> params=new HashMap<>();
        params.put("os",1);
        params.put("eventType","click");
        params.put("appId",reportDTO.getDspId());

        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            params.put("imei", reportDTO.getImeiSum());
        }else if(StringUtils.isNotBlank(reportDTO.getImei()) &&!StringUtils.contains(reportDTO.getImei(),"__")){
           params.put("imei", SecureUtil.md5(reportDTO.getImei()));
        }

        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            params.put("oaid", reportDTO.getOaidSum());
        }else if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            params.put("oaid", SecureUtil.md5(reportDTO.getOaid()));
        }


        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) &&!StringUtils.contains(reportDTO.getIdfaSum(),"__")){
            params.put("idfa", reportDTO.getIdfaSum());
        } else if(StringUtils.isNotBlank(reportDTO.getIdfa()) &&!StringUtils.contains(reportDTO.getIdfa(),"__")){
            params.put("idfa", SecureUtil.md5(reportDTO.getIdfa()));
        }
        String format = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("clickid",format);
        params.put("ip",reportDTO.getIp());
        if(StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            params.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        }

        String urlParams = UrlUtil.covertParamsOrderByAsc(params);
        return accountDTO.getDspReportUrl()+"?"+urlParams;
    }
}
